package org.example.controller;

import io.seata.spring.annotation.GlobalTransactional;
import org.example.pojo.Order;
import org.example.service.BookFeign;
import org.example.service.UserFeign;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author TonySong
 * @date 2024/7/9 0009
 * @time 16:29
 */
@RestController
public class HelloController {


    @Resource
    private BookFeign bookFeign;

    @Resource
    private UserFeign userFeign;

    @GetMapping("/buy")
    @GlobalTransactional
    public String buy(int uid,int bid,int num){
         // 1、根据bid：查询出商品
        Boolean stock = this.bookFeign.stock(bid, num);
        System.err.println("是否可以下单："+stock);
//        int m = 11/0;
//        String s1=null;
//        s1.length();//空指针
//        int[] arr={1,2,3};
//        int m=arr[3];
        Boolean pay =false;
        // 2、下单
        if(stock){
            Order order =new Order();
            order.setBid(bid);
            order.setUid(uid);
            order.setTotal(num);
            Boolean flag = this.userFeign.pay(order);
            System.out.println("flag:"+flag);
            pay = flag;
        }
        return pay ? "下单成功": "下单失败";

    }
}
